package cn.xy.base.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.xy.base.server.bean.IPage;
import cn.xy.base.server.po.SysAdmin;
import cn.xy.base.server.po.SysLog;
import cn.xy.base.server.service.SysAdminService;
import cn.xy.base.server.service.SysLogService;
import cn.xy.commons.core.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 表[sys_log]对应的后台管理类
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/admapi/sys/log")
@Api(tags = {"操作日志管理接口"})
public class SysLogAdminController {

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private SysAdminService sysAdminService;

    @ApiOperation(value = "操作日志-列表")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('system-sys-log-list')")
    public Result<Page<SysLog>> list(IPage page,
                                     @RequestParam(required = false, defaultValue = "") String userName,
                                     @RequestParam(required = false) @ApiParam(value = "开始时间(yyyy-MM-dd)") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                                     @RequestParam(required = false) @ApiParam(value = "结束时间(yyyy-MM-dd)") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        List<Integer> userIdList = new ArrayList<>();
        if (CharSequenceUtil.isNotEmpty(userName)) {
            List<SysAdmin> sysAdminList = sysAdminService.searchByNameOrMobile(userName);
            if (CollUtil.isEmpty(sysAdminList)) {
                return Result.data(new Page<>());
            }
            userIdList = sysAdminList.stream().map(SysAdmin::getId).collect(Collectors.toList());
        }
        LambdaQueryWrapper<SysLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CollUtil.isNotEmpty(userIdList), SysLog::getUserId, userIdList);
        log.info("数据===>{}",userIdList);
        queryWrapper.ge(startTime != null, SysLog::getCreateTime, startTime);
        queryWrapper.le(endTime != null, SysLog::getCreateTime, endTime);
        queryWrapper.orderByDesc(SysLog::getId);
        Page<SysLog> pageData = sysLogService.page(page.toPage(), queryWrapper);
        if (CollUtil.isEmpty(pageData.getRecords())) {
            return Result.data(new Page<>());
        }
        List<Integer> adminIdList = pageData.getRecords().stream().map(SysLog::getUserId).collect(Collectors.toList());
        List<SysAdmin> sysAdminList = sysAdminService.listByIdIn(adminIdList);
        Map<Integer, String> adminNameMap = sysAdminList.stream().collect(Collectors.toMap(SysAdmin::getId, SysAdmin::getNickname));
        for (SysLog sysLog : pageData.getRecords()) {
            sysLog.setUserName(adminNameMap.get(sysLog.getUserId()));
        }
        return Result.data(pageData);
    }

    @ApiOperation(value = "操作日志-详情")
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('system-sys-log-detail')")
    public Result<SysLog> detail(int id) {
        if (id <= 0) {
            return Result.error("操作日志不存在");
        }
        SysLog sysLog = sysLogService.getById(id);
        if (sysLog == null) {
            return Result.error("操作日志不存在");
        }
        return Result.data(sysLog);
    }
}
